package com.naton.common.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.ImageViewTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.naton.common.R
import com.naton.common.callback.ImageCompleteCallback
import com.naton.common.ui.CommApplication
import java.io.File
import java.util.concurrent.ExecutionException

/**
 * @time: 2022/1/17
 * @author: lifuling
 * @version: v1.0
 * @file_desc:图片相关工具类
 */
object ImageUtils {

    /**
     * 加载图片
     *
     * @param mContext  上下文
     * @param url       图片地址
     * @param iv_icon   承载图片的UI框架
     */
    fun loadImg(url : String,  iv_icon : ImageView){
        Glide.with(CommApplication.getInstance())
            .load(url)
            .placeholder(R.drawable.ps_image_placeholder)	// 展位图
            .into(iv_icon);
    }

    /**
     * 加载图片
     *
     * @param mContext  上下文
     * @param url       图片地址
     * @param iv_icon   承载图片的UI框架
     */
    fun loadImg(url : String,  iv_icon : ImageView , resId : Int){
        Glide.with(CommApplication.getInstance())
            .load(url)
            .placeholder(resId)	// 展位图
            .into(iv_icon);
    }

    /**
     * 加载圆角图片
     */
    fun loadImg(url : String , roundingRadius : Int , iv_icon : ImageView){
        //设置图片的圆角角度
        val roundedCorners = RoundedCorners(roundingRadius)
        //通过RequestOptions扩展功能
        val option = RequestOptions.bitmapTransform(roundedCorners).override(300 , 300)

        Glide.with(CommApplication.getInstance())
            .load(url)
            .apply(option)
            .placeholder(R.drawable.ps_image_placeholder)
            .into(iv_icon)
    }

    /**
     * 加载圆角图片
     */
    fun loadImg(url : String  , iv_icon : ImageView, roundingRadius : Int , resId : Int){
        //设置图片的圆角角度
        val roundedCorners = RoundedCorners(roundingRadius)
        //通过RequestOptions扩展功能
        val option = RequestOptions.bitmapTransform(roundedCorners).override(300 , 300)

        Glide.with(CommApplication.getInstance())
            .load(url)
            .apply(option)
            .placeholder(resId)
            .into(iv_icon)
    }

    /**
     * 加载圆形图片
     *
     * @param url       图片地址
     * @param iv_icon   承载图片的UI框架
     */
    fun loadCircleImg(url : String,  iv_icon : ImageView){
        Glide.with(CommApplication.getInstance())
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .placeholder(R.drawable.ps_image_placeholder)	// 展位图
            .into(iv_icon);
    }

    /**
     * 加载圆形图片
     *
     * @param url       图片地址
     * @param iv_icon   承载图片的UI框架
     */
    fun loadCircleImg(url : String,  iv_icon : ImageView , resId : Int){
        Glide.with(CommApplication.getInstance())
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .placeholder(resId)	// 展位图
            .into(iv_icon);
    }


    /**
     * 带进度的网络加载
     */
    fun loadImage(url: String?, imageView: ImageView, callback: ImageCompleteCallback) {
        Glide.with(CommApplication.getInstance())
            .asBitmap()
            .load(url)
            .placeholder(R.drawable.ps_image_placeholder)
            .into(object : ImageViewTarget<Bitmap>(imageView) {
                override fun onLoadStarted(placeholder: Drawable?) {
                    super.onLoadStarted(placeholder)
                    callback.onShowLoading()
                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    super.onResourceReady(resource, transition)
                    callback.onHideLoading()
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    callback.onHideLoading()
                }

                override fun setResource(resource: Bitmap?) {
                    imageView.setImageBitmap(resource)
                }
            })
    }

    /**
     * 加载gif
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    fun loadAsGifImage( url: String,imageView: ImageView) {
        Glide.with(CommApplication.getInstance())
            .asGif()
            .load(url)
            .placeholder(R.drawable.ps_image_placeholder)
            .into(imageView)
    }

    /**
     * 根据路径获取缓存文件
     * @param url
     * @return
     */
    fun getImagePathFromCache(url: String?): File? {
        var cacheFile: File? = null
        try {
            cacheFile = Glide.with(CommApplication.getInstance())
                .load(url)
                .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get()
        } catch (e: ExecutionException) {
            e.printStackTrace()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
        return cacheFile
    }

    /**
     * 清楚缓存
     */
    fun clearCache() {
        GlobalThreadPools.getInstance().execute {
            Glide.get(CommApplication.getInstance()).clearMemory()
            Glide.get(CommApplication.getInstance()).clearDiskCache()
        }
    }
}